import type { NextApiRequest, NextApiResponse } from "next";
import path from "node:path";
import fs from "node:fs";
import FormData from "form-data";
import axios from "axios";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method === "GET") {
    const dirPath = path.join(process.cwd(), "upload");
    const formData = new FormData();
    const imageNames = req.query.names as string;
    imageNames.split(",").forEach((imageName) => {
      formData.append(
        "images",
        fs.createReadStream(path.join(dirPath, imageName))
      );
    });
    await axios.post("http://localhost:3004/api/init", formData, {
      ...formData.getHeaders(),
    });
    return res.json({ code: 0, message: "图片传输完成" });
  }
}
